﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using FbKPI.Facade.Validator;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;
using System.Transactions;

namespace FBKPI.ProcessRequest
{
    /// <summary>
    /// Summary description for KPIHandler
    /// </summary>
    public class KPIHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string Action = string.Empty;
            string msg = string.Empty;
            Action = context.Request.QueryString["method"];
            if (Action == "addbyuser")
            {
                msg = this.SaveByUser();
            }
            else
            {
                msg = this.Save();
            }
            context.Response.Write(msg);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string Save()
        {
            string msg = "OK";
            string action = HttpContext.Current.Request.QueryString["action"];
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["ID"]);
            string Description = HttpContext.Current.Request.QueryString["Description"];
            bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            bool weightEnable = Convert.ToBoolean(HttpContext.Current.Request.QueryString["weightEnable"]);
            bool uomEnable = Convert.ToBoolean(HttpContext.Current.Request.QueryString["uomEnable"]);
            bool ScoredBySystem = Convert.ToBoolean(HttpContext.Current.Request.QueryString["ScoredBySystem"]);
            int GroupKPIID = HttpContext.Current.Request.QueryString["GroupKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["GroupKPI"]);
            int CategoryKPIID = HttpContext.Current.Request.QueryString["CategoryKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["CategoryKPI"]);
            int TypeKPIID = HttpContext.Current.Request.QueryString["TypeKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["TypeKPI"]);
            int SubTypeKPIID = HttpContext.Current.Request.QueryString["SubTypeKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SubTypeKPI"]);
            int UOMID = HttpContext.Current.Request.QueryString["UOM"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOM"]);
            int UOMMaxID = HttpContext.Current.Request.QueryString["UOMMax"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOMMax"]);
            int UOMMinID= HttpContext.Current.Request.QueryString["UOMMin"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOMMin"]);
            int KPIParentID = HttpContext.Current.Request.QueryString["KPIParent"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["KPIParent"]);
            int KPIReplacementID = HttpContext.Current.Request.QueryString["KPIReplacement"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["KPIReplacement"]);
            int Year = HttpContext.Current.Request.QueryString["Year"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Year"]);
            int Weight = HttpContext.Current.Request.QueryString["Weight"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Weight"]);
            string Periode = HttpContext.Current.Request.QueryString["Periode"];
            string Polarization = HttpContext.Current.Request.QueryString["Polarization"];
            string TargetDescription = string.Empty;// HttpContext.Current.Request.QueryString["TargetDescription"];
            int ManualCalculation = 0;/// HttpContext.Current.Request.QueryString["ManualCalculation"] == string.Empty ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["ManualCalculation"]);
            string CalculationType = string.Empty;// HttpContext.Current.Request.QueryString["CalculationType"];
            DateTime? ReplacementDate = null;// HttpContext.Current.Request.QueryString["ReplacementDate"] == string.Empty ? (DateTime?)null : Convert.ToDateTime(HttpContext.Current.Request.QueryString["ReplacementDate"]);
            string ReplacementDescription = string.Empty;// HttpContext.Current.Request.QueryString["ReplacementDescription"];
            string DetailInformation = string.Empty;// HttpContext.Current.Request.QueryString["DetailInformation"];
            string StrategicObjective = string.Empty;// HttpContext.Current.Request.QueryString["StrategicObjective"];
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            int Sequence = HttpContext.Current.Request.QueryString["Sequence"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Sequence"]);
            int EmployeeIDVerifier = HttpContext.Current.Request.QueryString["EmployeeIDVerifier"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["EmployeeIDVerifier"]);
            bool isLockWeight = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isLockWeight"]);
            string AssignWeight = HttpContext.Current.Request.QueryString["AssignWeight"];
            int PercentageMax = Convert.ToInt16(HttpContext.Current.Request.QueryString["PercentageMax"]);
            int PercentageMin = Convert.ToInt16(HttpContext.Current.Request.QueryString["PercentageMin"]);
            int SignEmployeeID = HttpContext.Current.Request.QueryString["SignEmployeeID"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SignEmployeeID"]);

            KPIFacade df = new KPIFacade();
            KPI KPIs = df.GetKPIByID(ID);
            KPIValidator dv = new KPIValidator();

            string FlagInsertKPIUser = HttpContext.Current.Request.Params["FlagInsertKPIUser"];

            if (KPIs == null)
            {
                KPIs = new KPI();
                KPIs.ID = ID;
                KPIs.Description = Description;
                KPIs.isActive = isActive;
                KPIs.WeightEnableFlag = weightEnable;
                KPIs.UoMEnableFlag = uomEnable;
                KPIs.ScoredBySystem = ScoredBySystem;
                KPIs.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(GroupKPIID);
                KPIs.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(CategoryKPIID);
                KPIs.TypeKPI = new TypeKPIFacade().GetTypeKPIByID(TypeKPIID);
                KPIs.SubTypeKPI = new SubTypeKPIFacade().GetSubTypeKPIByID(SubTypeKPIID);
                KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
                KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
                KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
                KPIs.KPIParent = new KPIFacade().GetKPIByID(KPIParentID);
                KPIs.KPIReplacement = new KPIFacade().GetKPIByID(KPIReplacementID);
                KPIs.Year = Year;
                KPIs.Weight = Weight;
                KPIs.Periode = Periode;
                KPIs.Polarization = Polarization;
                KPIs.TargetDescription = TargetDescription;
                KPIs.ManualCalculation = ManualCalculation;
                KPIs.KPICalculationType = CalculationType;
                KPIs.ReplacementDate = ReplacementDate;
                KPIs.ReplacementDescription = ReplacementDescription;
                KPIs.KPIDetailInformation = DetailInformation;
                KPIs.KPIStrategicObjective = StrategicObjective;
                KPIs.CreateBy = CurrUser.Email;
                KPIs.Sequence = Sequence;
                KPIs.EmployeeIDVerifier = EmployeeIDVerifier;
                KPIs.BusinessGroupID = CurrUser.BusinessGroupID;
                KPIs.isLockWeight = isLockWeight;
                KPIs.AssignWeight = AssignWeight;
                KPIs.PercentageMax = PercentageMax;
                KPIs.PercentageMin = PercentageMin;
                KPIs.SignEmployeeID = SignEmployeeID;
                msg = dv.ValidateOnSave(KPIs);
            }
            else if (action != "delete")
            {
                KPIs.Description = Description;
                KPIs.isActive = isActive;
                KPIs.WeightEnableFlag = weightEnable;
                KPIs.UoMEnableFlag = uomEnable;
                KPIs.ScoredBySystem = ScoredBySystem;
                KPIs.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(GroupKPIID);
                KPIs.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(CategoryKPIID);
                KPIs.TypeKPI = new TypeKPIFacade().GetTypeKPIByID(TypeKPIID);
                KPIs.SubTypeKPI = new SubTypeKPIFacade().GetSubTypeKPIByID(SubTypeKPIID);
                KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
                KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
                KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
                KPIs.KPIParent = new KPIFacade().GetKPIByID(KPIParentID);
                KPIs.KPIReplacement = new KPIFacade().GetKPIByID(KPIReplacementID);
                KPIs.Year = Year;
                KPIs.Weight = Weight;
                KPIs.Periode = Periode;
                KPIs.Polarization = Polarization;
                KPIs.TargetDescription = TargetDescription;
                KPIs.ManualCalculation = ManualCalculation;
                KPIs.KPICalculationType = CalculationType;
                KPIs.ReplacementDate = ReplacementDate;
                KPIs.ReplacementDescription = ReplacementDescription;
                KPIs.KPIDetailInformation = DetailInformation;
                KPIs.KPIStrategicObjective = StrategicObjective;
                KPIs.LastModifiedBy = CurrUser.Email;
                KPIs.Sequence = Sequence;
                KPIs.EmployeeIDVerifier = EmployeeIDVerifier;
                KPIs.isLockWeight = isLockWeight;
                KPIs.AssignWeight = AssignWeight;
                KPIs.PercentageMax = PercentageMax;
                KPIs.PercentageMin = PercentageMin;
                KPIs.SignEmployeeID = SignEmployeeID;
                msg = dv.ValidateOnUpdate(KPIs);

            }
            else if(action == "delete")
                KPIs.LastModifiedBy = CurrUser.Email;


            if (msg == "OK")
            {
                using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    switch (action)
                    {
                        case "save":
                            msg = df.Save(KPIs, FlagInsertKPIUser);
                            break;
                        case "delete":
                            msg = df.BeforeDelete(KPIs);
                            if (msg == "0")
                                msg = df.Delete(KPIs);
                            else
                                msg = "Data tidak bisa di hapus,dipakai pada tabel yang lain !";
                            break;
                    }
                    trans.Complete();
                    trans.Dispose();
                }
            }

            return msg;
        }


        private string SaveByUser()
        {
            string msg = "OK";
            string action = HttpContext.Current.Request.QueryString["action"];
            int ID = Convert.ToInt16(HttpContext.Current.Request.QueryString["ID"]);
            string Description = HttpContext.Current.Request.QueryString["Description"];
            bool isActive = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isActive"]);
            bool weightEnable = Convert.ToBoolean(HttpContext.Current.Request.QueryString["weightEnable"]);
            bool uomEnable = Convert.ToBoolean(HttpContext.Current.Request.QueryString["uomEnable"]);
            bool ScoredBySystem = Convert.ToBoolean(HttpContext.Current.Request.QueryString["ScoredBySystem"]);
            int GroupKPIID = HttpContext.Current.Request.QueryString["GroupKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["GroupKPI"]);
            int CategoryKPIID = HttpContext.Current.Request.QueryString["CategoryKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["CategoryKPI"]);
            int TypeKPIID = HttpContext.Current.Request.QueryString["TypeKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["TypeKPI"]);
            int SubTypeKPIID = HttpContext.Current.Request.QueryString["SubTypeKPI"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["SubTypeKPI"]);
            int UOMID = HttpContext.Current.Request.QueryString["UOM"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOM"]);
            int UOMMaxID = HttpContext.Current.Request.QueryString["UOMMax"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOMMax"]);
            int UOMMinID = HttpContext.Current.Request.QueryString["UOMMin"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["UOMMin"]);
            int KPIParentID = HttpContext.Current.Request.QueryString["KPIParent"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["KPIParent"]);
            int KPIReplacementID = HttpContext.Current.Request.QueryString["KPIReplacement"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["KPIReplacement"]);
            int Year = HttpContext.Current.Request.QueryString["Year"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Year"]);
            int Weight = HttpContext.Current.Request.QueryString["Weight"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Weight"]);
            string Periode = HttpContext.Current.Request.QueryString["Periode"];
            string Polarization = HttpContext.Current.Request.QueryString["Polarization"];
            string TargetDescription = string.Empty;// HttpContext.Current.Request.QueryString["TargetDescription"];
            int ManualCalculation = 0;/// HttpContext.Current.Request.QueryString["ManualCalculation"] == string.Empty ? 0 : Convert.ToInt32(HttpContext.Current.Request.QueryString["ManualCalculation"]);
            string CalculationType = string.Empty;// HttpContext.Current.Request.QueryString["CalculationType"];
            DateTime? ReplacementDate = null;// HttpContext.Current.Request.QueryString["ReplacementDate"] == string.Empty ? (DateTime?)null : Convert.ToDateTime(HttpContext.Current.Request.QueryString["ReplacementDate"]);
            string ReplacementDescription = string.Empty;// HttpContext.Current.Request.QueryString["ReplacementDescription"];
            string DetailInformation = string.Empty;// HttpContext.Current.Request.QueryString["DetailInformation"];
            string StrategicObjective = string.Empty;// HttpContext.Current.Request.QueryString["StrategicObjective"];
            Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["CurrUser"]));
            int Sequence = HttpContext.Current.Request.QueryString["Sequence"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["Sequence"]);
            int EmployeeIDVerifier = HttpContext.Current.Request.QueryString["EmployeeIDVerifier"] == string.Empty ? 0 : Convert.ToInt16(HttpContext.Current.Request.QueryString["EmployeeIDVerifier"]);
            bool isLockWeight = Convert.ToBoolean(HttpContext.Current.Request.QueryString["isLockWeight"]);
            string AssignWeight = HttpContext.Current.Request.QueryString["AssignWeight"];
            int PercentageMax = Convert.ToInt16(HttpContext.Current.Request.QueryString["PercentageMax"]);
            int PercentageMin = Convert.ToInt16(HttpContext.Current.Request.QueryString["PercentageMin"]);

            KPIFacade df = new KPIFacade();
            KPI KPIs = df.GetKPIByID(ID);
            KPIValidator dv = new KPIValidator();

            string FlagInsertKPIUser = HttpContext.Current.Request.Params["FlagInsertKPIUser"];

            if (KPIs == null)
            {
                KPIs = new KPI();
                KPIs.ID = ID;
                KPIs.Description = Description;
                KPIs.isActive = isActive;
                KPIs.WeightEnableFlag = weightEnable;
                KPIs.UoMEnableFlag = uomEnable;
                KPIs.ScoredBySystem = ScoredBySystem;
                KPIs.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(GroupKPIID);
                KPIs.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(CategoryKPIID);
                KPIs.TypeKPI = new TypeKPIFacade().GetTypeKPIByID(TypeKPIID);
                KPIs.SubTypeKPI = new SubTypeKPIFacade().GetSubTypeKPIByID(SubTypeKPIID);
                KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
                KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
                KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
                KPIs.KPIParent = new KPIFacade().GetKPIByID(KPIParentID);
                KPIs.KPIReplacement = new KPIFacade().GetKPIByID(KPIReplacementID);
                KPIs.Year = Year;
                KPIs.Weight = Weight;
                KPIs.Periode = Periode;
                KPIs.Polarization = Polarization;
                KPIs.TargetDescription = TargetDescription;
                KPIs.ManualCalculation = ManualCalculation;
                KPIs.KPICalculationType = CalculationType;
                KPIs.ReplacementDate = ReplacementDate;
                KPIs.ReplacementDescription = ReplacementDescription;
                KPIs.KPIDetailInformation = DetailInformation;
                KPIs.KPIStrategicObjective = StrategicObjective;
                KPIs.CreateBy = CurrUser.Email;
                KPIs.Sequence = Sequence;
                KPIs.EmployeeIDVerifier = EmployeeIDVerifier;
                KPIs.BusinessGroupID = CurrUser.BusinessGroupID;
                KPIs.isLockWeight = isLockWeight;
                KPIs.AssignWeight = AssignWeight;
                KPIs.PercentageMax = PercentageMax;
                KPIs.PercentageMin = PercentageMin;
                msg = dv.ValidateOnSave(KPIs);
            }
            else if (action != "delete")
            {
                KPIs.Description = Description;
                KPIs.isActive = isActive;
                KPIs.WeightEnableFlag = weightEnable;
                KPIs.UoMEnableFlag = uomEnable;
                KPIs.ScoredBySystem = ScoredBySystem;
                KPIs.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(GroupKPIID);
                KPIs.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(CategoryKPIID);
                KPIs.TypeKPI = new TypeKPIFacade().GetTypeKPIByID(TypeKPIID);
                KPIs.SubTypeKPI = new SubTypeKPIFacade().GetSubTypeKPIByID(SubTypeKPIID);
                KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
                KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
                KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
                KPIs.KPIParent = new KPIFacade().GetKPIByID(KPIParentID);
                KPIs.KPIReplacement = new KPIFacade().GetKPIByID(KPIReplacementID);
                KPIs.Year = Year;
                KPIs.Weight = Weight;
                KPIs.Periode = Periode;
                KPIs.Polarization = Polarization;
                KPIs.TargetDescription = TargetDescription;
                KPIs.ManualCalculation = ManualCalculation;
                KPIs.KPICalculationType = CalculationType;
                KPIs.ReplacementDate = ReplacementDate;
                KPIs.ReplacementDescription = ReplacementDescription;
                KPIs.KPIDetailInformation = DetailInformation;
                KPIs.KPIStrategicObjective = StrategicObjective;
                KPIs.LastModifiedBy = CurrUser.Email;
                KPIs.Sequence = Sequence;
                KPIs.EmployeeIDVerifier = EmployeeIDVerifier;
                KPIs.isLockWeight = isLockWeight;
                KPIs.AssignWeight = AssignWeight;
                KPIs.PercentageMax = PercentageMax;
                KPIs.PercentageMin = PercentageMin;
                msg = dv.ValidateOnUpdate(KPIs);

            }
            else if (action == "delete")
                KPIs.LastModifiedBy = CurrUser.Email;


            if (msg == "OK")
            {
                using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    switch (action)
                    {
                        case "save":
                            if (df.SaveKPIByUser(KPIs, CurrUser))
                            {
                                msg = "Transaction Success";
                            }
                            else
                                msg = "Transaction Failed";
                            break;
                        case "delete":
                            msg = df.BeforeDelete(KPIs);
                            if (msg == "0")
                                msg = df.Delete(KPIs);
                            else
                                msg = "Data tidak bisa di hapus,dipakai pada tabel yang lain !";
                            break;
                    }
                    trans.Complete();
                    trans.Dispose();
                }
            }

            return msg;
        }
    }
}